class KeyWatcher {
  _callback;
  _listening;
  constructor(callback) {
    this._callback = callback;
    this._listening = false;
  }
  start() {
    if (this._listening) {
      return;
    }
    this._listening = true;
    process.stdin.on("data", (key) => {
      const char = key.toString();
      this._callback();
      if (char === "") {
        process.exit();
      }
    });
  }
  stop() {
    if (!this._listening) {
      return;
    }
    this._listening = false;
    process.stdin.pause();
    process.stdin.removeAllListeners("data");
  }
}

export { KeyWatcher };
